﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMail.Common.Interfaces.Data;

namespace AMail.Modules.Data.Actions
{
  public class AddMessagesAction : IMailAction
  {
    private List<IMailMessage> newMessages;

    public AddMessagesAction(IEnumerable<IMailMessage> messages)
    {
      this.newMessages = messages.ToList();
    }

    #region IMailAction Members

    public void Commit(IMailArchiver archiver)
    {
      foreach (IMailMessage message in newMessages)
      {
        if (message.IsNew)
          archiver.AddMessage(message);
      }
    }

    public void Undo()
    {
      foreach (var message in newMessages)
        message.ParentFolder = null;
    }

    #endregion

    public override string ToString()
    {
      if (this.newMessages.Count == 1)
        return string.Format("Added message '{0}' to '{1}'", this.newMessages[0].Subject, this.newMessages[0].ParentFolder.Name);
      else
        return string.Format("Added {0} messages to '{1}'", this.newMessages.Count, this.newMessages[0].ParentFolder.Name);
    }

  }
}
